const app = getApp();
import {
  myCouponList,getOneId,  getWxUser,
  getOpenId,
} from "../../../server/api";
import {parseTime} from '../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oss_url: app.globalData.oss_url,
    active: 0,
    // isshow:false,
    pageSize: 10,
    pageNum: 1,
    oneId:"",
    list: [],
    isFixedTop: false,
    have_more: true,
    activeTit:[
      { 
        id:'',
        title:'全部',
      },
      { 
        id:0,
        title:'直抵券',
      },
      { 
        id:1,
        title:'兑换券',
      },
      { 
        id:2,
        title:'满减券',
      },
      { 
        id:3,
        title:'折扣券',
      },
    ]
  },
  //tabs 切换
  onChange(e) {
    this.setData({
      list:[]
    })
    let index=e.detail.name
    switch (index) {
      case 0:
        this.getList()
        break;
      case 1:
        this.getList(0)
        break;
      case 2:
        this.getList(1)
        break;
      case 3:
        this.getList(2)
        break;
      case 4:
        this.getList(3)
        break;
    }
  },
  //查看详细信息
  detailedFun(e){
    var actId=e.currentTarget.dataset.clickitem.activityId
    this.setData({
      list:this.data.list.map(v=>{
        if(v.activityId==actId){
           v.isshow=!v.isshow
        }
        return v
      })
    })
    // console.log(this.data.list,"查看详细信息")
  },
  copy: function (e) {
    let that = this;
    wx.setClipboardData({
      //去找上面的数据
      data: e.currentTarget.dataset.code,
      success: function (res) {
        wx.showToast({
          title: '券码复制成功',
          icon:'none'
        });
      }
    });
  },
  //码卷 列表请求
  getList(couponType='') {
    if (this.data.submit_loading || !this.data.have_more) {
      return
    }
    let obj = {
      couponType,
      oneId:app.globalData.user_info.oneId
    }
    // let obj = {
    //   couponType,
    //   oneId:236
    // }
    // let obj = {
    //   couponType,
    //   oneId:1
    // }

    this.data.submit_loading = true
    myCouponList(obj).then(res => {
      this.data.submit_loading = false
      if (res.code == 0) {
        let arr = res.data
        arr=arr.map((v,i)=>{
          // if(i==0){
          //   v.couponStatus=1
          //   v.couponRelated.failureTime="2021-03-22T13:08:31.000+0000"
          // }
          v.isshow=false
          v.couponRelated.failureTime=this.timeChn(v.couponRelated.failureTime)
          return v
        })
       //  arr.push(...res.data.list)
        this.setData({ list: arr })
       // console.log(this.data.list,"码卷列表")
      }
    })
  },
  goMiniProgram(){
    wx.navigateToMiniProgram({
      appId: 'wxac02a0069430772a',
      path: '',
      // extraData: {
      //   foo: 'bar'
      // },
      envVersion: 'release',
      success(res) {
       // console.log('打开成功')
      },
      fail(err){
        console.log('err',err)
      }
    })
  },
  //活动日期 格式化
  timeChn(val) {
    if (!val) {
      return ''
    }
    // val=val.split('T')[0]
    var realTime = new Date(val)
    var nowTime = new Date()

    var diffValue = nowTime - realTime
    var _min = diffValue / (60 * 1000)
    var days = parseInt(diffValue / (1000 * 60 * 60 * 24));
    var hours = parseInt((diffValue % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = parseInt((diffValue % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = (diffValue % (1000 * 60)) / 1000;
    // console.log(val,diffValue,_min,days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 ")
      if(days==0 && hours>=0){
        return '今日过期'
      }else if(days<0 || hours<-1){
          return '有效期至'+parseTime(val,'{y}-{m}-{d}');
      }else{
          return '已过期'
          // return '有效期至'+val.split('T')[0]
      }
    // if (_min < 1) {
    //   return '今日过期'
    // } else if (_min >= 1 && _min < 10) {
    //   return '1分钟前'
    // } else if (_min >= 10 && _min < 30) {
    //   return '10分钟前'
    // } else if (_min >= 30 && _min < 60) {
    //   return '半小时前'
    // } else if (_min >= 60 && _min < 60 * 24) {
    //   return Math.floor(_min / 60) + '小时前'
    // } else if (_min >= 60 * 24 && _min < 60 * 24 * 7) {
    //   return Math.floor(_min / (60 * 24)) + '天前'
    // } else {
    //   return val
    // }
  },
  //获取oneID
  get_openId() {
    wx.login({
      success(res) {
        if (res.code) {
          let obj = {}
          obj['jsCode'] = res.code
          getOpenId(obj).then(res_openId => {
            if (res_openId.code == 0) {
              app.globalData.user_info.openId = res_openId.data.openid
              app.globalData.session_key = res_openId.data.session_key
              let obj = {}
              obj['openId'] = app.globalData.user_info.openId
              getWxUser(obj).then(res_user => {
                if (res_user.data) {
                  app.globalData.user_info.avatarUrl = res_user.data.avatarUrl
                  app.globalData.user_info.nickName = res_user.data.nickName
                  app.globalData.user_info.openId = res_user.data.openId
                  app.globalData.user_info.phone = res_user.data.phone
                }
              });
              getOneId(obj).then(res => {
                if (res.code == 0) {
                  app.globalData.user_info.oneId = res.data

                }
              })
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '我的券码',
    })
  },
  onPageScroll: function (e) {
    if (e.scrollTop >= 300) {
      this.setData({
        isOperate: true,
      })
    } else {
      this.setData({
        isOperate: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.setData({
        pageSize : 10,
        pageNum : 1,
        submit_loading : false,
        have_more: true,
      })
      this.get_openId()
      //请求卷码列表
      this.getList()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.pageNum++
    console.log("触底")
    // this.get_list_active()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})